package com.zzyl.service.impl;

import com.zzyl.dto.LoginDto;
import com.zzyl.mapper.LoginMapper;
import com.zzyl.service.LoginService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    /**
     * 登录
     * @param loginDto
     * @return
     */
    public UserVo login(LoginDto loginDto) {
        UserVo userVo = loginMapper.login(loginDto);

        if(userVo ==null){
            //否则登录失败
            throw new RuntimeException("用户名或密码错误");
        }
        //如果查询不为空，则生成令牌
        Map<String, Object> map = new HashMap<>();
        map.put("username",loginDto.getUsername());
        map.put("userId",userVo.getId());
        map.put("userType",userVo.getUserType());



        userVo.setUserToken(JwtUtil.createJWT("itheima",600000,map));


        return userVo;
    }
}
